package com.khoayang.littlefighterchaos;

import org.cocos2d.nodes.CCDirector;
import org.cocos2d.opengl.CCGLSurfaceView;
import org.cocos2d.sound.SoundEngine;

import com.khoayang.littlefighterchaos.game.CharacterFactory;
import com.khoayang.littlefighterchaos.game.GamePad;
import com.khoayang.littlefighterchaos.menu.MainMenuScence;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity implements SensorEventListener {
	protected CCGLSurfaceView _glSurfaceView;
	
	private SensorManager sensorManager;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
	    super.onCreate(savedInstanceState);
        
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	 
	    _glSurfaceView = new CCGLSurfaceView(this);
	 
	    setContentView(_glSurfaceView);
	    CharacterFactory characterFactory = CharacterFactory.getInstance(getApplicationContext());
		characterFactory.initAllCharacter(getAssets(), "character/Character.xml");
	
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_NORMAL);
		
		
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onStart()
    {
        super.onStart();
     
        CCDirector.sharedDirector().attachInView(_glSurfaceView);
        
//        CCDirector.sharedDirector().setDeviceOrientation(
//				CCDirector.kCCDeviceOrientationLandscapeLeft);
     
        CCDirector.sharedDirector().setDisplayFPS(true);
     
        CCDirector.sharedDirector().setAnimationInterval(1.0f / 60.0f);
        
        MainMenuScence menuScene = new MainMenuScence(getApplicationContext());
        CCDirector.sharedDirector().runWithScene(menuScene);
    }
    
    @Override
    protected void onPause() {
    	
    	SoundEngine.sharedEngine().pauseSound();
        CCDirector.sharedDirector().purgeCachedData();
        _glSurfaceView.onPause();
        CCDirector.sharedDirector().pause();
    	super.onPause();
    }
     
    @Override
    public void onResume()
    {
        sensorManager.registerListener(this,
				sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				SensorManager.SENSOR_DELAY_NORMAL);
        _glSurfaceView.onResume();
        CCDirector.sharedDirector().resume();
        super.onResume();
    }
     
    @Override
    public void onStop()
    {
    	sensorManager.unregisterListener(this);
        
        CCDirector.sharedDirector().end();
        super.onStop();
     
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        Log.d("Activity", "key down");

        CCDirector.sharedDirector().onKeyDown(event);
        return super.onKeyDown(keyCode, event);
    }
    
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
    	Log.d("Activity", "key up");

        CCDirector.sharedDirector().onKeyDown(event);
        return super.onKeyDown(keyCode, event);
    }

	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// TODO Auto-generated method stub
		
	}

	public void onSensorChanged(SensorEvent event) {
		// TODO Auto-generated method stub
		GamePad.HandleSensor(event);
	}
    
}
